import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:xianyu_school/utils/url.dart';
import 'package:xianyu_school/xianyuApp/entity/userData/data.dart';
import 'package:xianyu_school/xianyuApp/mine/settingPage.dart';

import 'BallPulseIndicator.dart';


class mineHeader extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    var url = urls().url;
    data data1 = new data();

    Future gethttp() async {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      data1.username = prefs.getString("username");
      try{
        Response response = await Dio().get(url + "/users/data",queryParameters:{"username":data1.username});
        return response;
      }
      catch(e){
        return ;
      }
    }

    return FutureBuilder(
        future: gethttp(),
        builder: (context, snapshot){
          if(snapshot.hasData){
            Map resultmap = json.decode(snapshot.data.toString());
            data1 = data.fromJson(resultmap);
            return Stack(
              children: [
                buildBottomWrap(),
                Padding(
                    padding: EdgeInsets.only(top: 30),
                    child: Container(
                      padding: EdgeInsets.only(left: 20, right: 10),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                            child: Image.memory(
                              base64.decode(data1.img),
                              fit: BoxFit.fill,
                              gaplessPlayback:true,
                              height: 70,
                              width: 70,
                            ),
                          ),
                          SizedBox(
                            width: 10,
                          ),
                          // context
                          Expanded(
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: <Widget>[
                                Text(
                                    data1.username,
                                    style: TextStyle(
                                      color: Colors.black,
                                      fontSize: 16,
                                    )
                                ),
                                SizedBox(
                                  height: 4,
                                ),
                                Text(
                                    "昵称: "+data1.nickname,
                                    style: TextStyle(
                                        color: Colors.black87,
                                        fontSize: 14
                                    )
                                ),
                                SizedBox(
                                  height: 4,
                                ),
                                Container(
                                  padding: EdgeInsets.symmetric(horizontal: 6),
                                  height: 20,
                                  width: 78,
                                  decoration: BoxDecoration(
                                    color: Colors.white,
                                    borderRadius: BorderRadius.circular(10),
                                  ),
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                    children: <Widget>[
                                      Text(
                                        "个人主页",
                                        style: TextStyle(
                                          color: Colors.black,
                                          fontSize: 13,
                                        ),
                                      ),
                                      Icon(
                                        Icons.arrow_forward_ios,
                                        size: 10,
                                        color: Colors.black,
                                      )
                                    ],
                                  ),
                                ),
                              ],
                            ),
                          ),

                          GestureDetector(
                            onTap: (){
                              Navigator.push(context, MaterialPageRoute(builder: (_) {
                                return settingPage();
                              }));
                            },
                            child: Container(
                              child: Icon(
                                Icons.settings,
                                size: 30,
                              ),
                            ),
                          )
                        ],
                      ),
                    )
                )
              ],
            );
          }
          else return BallPulseIndicator(
              color: Colors.white
          );;
        });
  }

  Widget buildBottomWrap() {
    return Container(
      height: 140,
      decoration: BoxDecoration(
          color: Colors.yellow,
          borderRadius: BorderRadius.only(
              bottomLeft: Radius.circular(10),
              bottomRight: Radius.circular(10)
          )
      ),
    );
  }
}